package com.cpi.basilos.corntype.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.cpi.basilos.corntype.dto.CornTypeDTO;
import com.cpi.basilos.corntype.exception.CornTypeException;
import com.cs.common.pool.DBDAO;
/**
 * Corn Type DAO
 * @author joao
 *
 */
public class CornTypeDAO extends DBDAO {
	
	private static final String SQL_GET_CORN_TYPE_BY_ID = "select nombre_corto, descripcion, caracteristicas, codigo_baan from TIPO_GRANO where id_tipograno = ?";//$NON-NLS-1$
	private static org.apache.log4j.Logger logger = Logger.getLogger(CornTypeDAO.class);
	
	public CornTypeDTO cornTypeList(int cornTypeId) throws CornTypeException{
		PreparedStatement preparedStatement =  null;
		CornTypeDTO dto = null;
		try{
			StringBuffer sql = new StringBuffer(SQL_GET_CORN_TYPE_BY_ID.length());
			sql.append(SQL_GET_CORN_TYPE_BY_ID);
			preparedStatement =  prepareSQL(sql.toString(), dsCIMa);			
			preparedStatement.setInt(1, cornTypeId);					
			ResultSet rst = executeQuery(preparedStatement);
			if (rst.next()){
				dto = new CornTypeDTO();
				dto.setCornTypeId(cornTypeId);
				dto.setShortName(rst.getString(1));
				dto.setDescription(rst.getString(2));
				dto.setFeatures(rst.getString(3));
				dto.setBaanCode(rst.getString(4));
			}						
		} catch (SQLException sqlException) {
			CornTypeException cornTypeException = null;
			logger.error(sqlException);
			sqlException.printStackTrace(System.err);
			cornTypeException = new CornTypeException(sqlException,CornTypeException.LAYER_DAO, CornTypeException.ACTION_SELECT);
			throw cornTypeException;
		} catch (Exception exception) {
			CornTypeException cornTypeException = null;
			logger.error(exception);
			exception.printStackTrace(System.err);
			cornTypeException = new CornTypeException(exception,CornTypeException.LAYER_DAO, CornTypeException.ACTION_SELECT);
			throw cornTypeException;
		}finally {
			try{
				closeConnection(preparedStatement.getConnection());
			}catch(Exception exception){
			}
		}		
		return dto;
		
	}

}
